package com.example.workbench.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.workbench.common.api.CommonResult;
import com.example.workbench.common.command.AddLineCommand;
import com.example.workbench.common.command.UpdateLineCommand;
import com.example.workbench.common.command.UpdateSortCommand;
import com.example.workbench.common.config.RedisConfig;
import com.example.workbench.common.config.shiro.UserUtil;
import com.example.workbench.common.vo.LineListVo;
import com.example.workbench.entity.DbTaskLine;
import com.example.workbench.mapper.DbTaskLineMapper;
import com.example.workbench.service.IDbTaskLineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-08-03
 */
@Service
public class DbTaskLineServiceImpl extends ServiceImpl<DbTaskLineMapper, DbTaskLine> implements IDbTaskLineService {

    @Override
    public CommonResult<String> addLine(AddLineCommand command) {
        DbTaskLine dbTaskLine = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskLine>()
                .eq(DbTaskLine::getTaskId, command.getTaskId())
                .eq(DbTaskLine::getLineName, command.getLineName())
                .eq(DbTaskLine::getIsDelete, 0));
        if (null != dbTaskLine) {
            return CommonResult.failed("该列表已存在");
        }
        Long count = getBaseMapper().selectCount(new LambdaQueryWrapper<DbTaskLine>()
                .eq(DbTaskLine::getTaskId, command.getTaskId())
                .eq(DbTaskLine::getIsDelete,0));
        DbTaskLine addLine = new DbTaskLine(command.getTaskId(),command.getLineName(), (int) (count+1));
        getBaseMapper().insert(addLine);
        return CommonResult.success("");
    }

    @Override
    public List<DbTaskLine> queryLine(Long id) {
        return getBaseMapper().selectList(new LambdaQueryWrapper<DbTaskLine>()
                .eq(DbTaskLine::getTaskId, id)
                .eq(DbTaskLine::getIsDelete, 0)
                .orderByAsc(DbTaskLine::getSort));
    }

    @Override
    public CommonResult<String> updateLine(UpdateLineCommand command) {
        DbTaskLine dbTaskLine = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskLine>()
                .eq(DbTaskLine::getId, command.getId())
                .eq(DbTaskLine::getIsDelete, 0));
        if (null == dbTaskLine) {
            return CommonResult.failed("该列表不存在");
        }
        dbTaskLine.setLineName(command.getLineName());
        getBaseMapper().updateById(dbTaskLine);
        return CommonResult.success("");
    }

    @Override
    public CommonResult<String> delLine(Long id) {
        DbTaskLine dbTaskLine = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskLine>()
                .eq(DbTaskLine::getId, id)
                .eq(DbTaskLine::getIsDelete, 0));
        if (null == dbTaskLine) {
            return CommonResult.failed("该列表不存在");
        }
        getBaseMapper().deleteById(dbTaskLine);
        return CommonResult.success("");
    }

    @Override
    public CommonResult<String> updateSort(UpdateSortCommand command) {
        int i = 1;
        for (LineListVo lineListVo : command.getList()) {
            DbTaskLine dbTaskLine = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskLine>()
                    .eq(DbTaskLine::getId, lineListVo.getId())
                    .eq(DbTaskLine::getIsDelete, 0));
            dbTaskLine.setSort(i);
            getBaseMapper().updateById(dbTaskLine);
            i++;
        }
        return CommonResult.success("");
    }
}
